package com.action;
/**
 * 通用文件上传
 * @auth lgh
 */

import com.bean.ResponseBean2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@RestController
public class FileUploadAction {

    @Value("${upload.tmp.dir}")
    private String tmp;

    @PostMapping("/upload")
    @CrossOrigin("*")
    public ResponseBean2 upload(MultipartFile  file) throws IllegalStateException, IOException
    {


        //得到旧的文件名  aaa.png
        String oldFileName = file.getOriginalFilename();
        //得到原来的后辍名
        int index = oldFileName.lastIndexOf('.');
        String extName = oldFileName.substring(index); //.png
        //根据系统时间拼一个当前新的文件名
        String newFileName = System.currentTimeMillis()+extName;
        //目的地
        File descFile = new File(tmp, newFileName);
        file.transferTo(descFile);

        return new ResponseBean2(0, "上传成功", newFileName);
    }

}
